package cn.stylefeng.guns.modular.business.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author 刘耀
 * @Date 2022/2/25 16:38
 */
public class TestJson {

    public static void main(String[] args) {
        //json字符串(只支持对象类型 不支持数组类型)
        String str = "{\n" +
                "    \"id\":\"主键 long\",\n" +
                "    \"name\":\"名称 str\",\n" +
                "    \"createTime\":\"double 时间\"\n" +
                "}";
        StringBuffer stringBuffer = new StringBuffer();
        LinkedHashMap<String, String> jsonMap = JSON.parseObject(str.replace(" ", ""
                .replace("\n", "")), new TypeReference<LinkedHashMap<String, String>>() {
        });
        for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
            String o = entry.getValue();
            String cur = entry.getKey();
            if (o.toLowerCase().contains("int")) {
                stringBuffer.append("/**\n* " + o.replace("int", "") + "\n*/\nprivate Integer " + cur + ";\n\n");
            } else if (o.toLowerCase().contains("long")) {
                stringBuffer.append("/**\n* " + o.replace("long", "") + "\n*/\nprivate Long " + cur + ";\n\n");
            } else if (o.toLowerCase().contains("str")) {
                stringBuffer.append("/**\n* " + o.replace("str", "") + "\n*/\nprivate String " + cur + ";\n\n");
            } else if (o.toLowerCase().contains("date") ) {
                stringBuffer.append("/**\n* " + o.replace("date", "") + "\n*/\nprivate Date " + cur + ";\n\n");
            }
            else if (o.toLowerCase().contains("datetime")) {
                stringBuffer.append("/**\n* " + o.replace("datetime", "") + "\n*/\nprivate Date " + cur + ";\n\n");
            }else if (o.toLowerCase().contains("double")) {
                stringBuffer.append("/**\n* " + o.replace("double", "") + "\n*/\nprivate BigDecimal " + cur + ";\n\n");
            } else {
                stringBuffer.append("/**\n* " + o + "\n*/\nprivate String " + cur + ";\n\n");
            }
        }
        System.out.println(stringBuffer.toString());
    }


}
